package com.dzsw.service.impl;

import com.dzsw.aspect.Neo4j;
import com.dzsw.entity.Dept;
import com.dzsw.entity.RelationShip;
import com.dzsw.repository.DeptRepository;
import com.dzsw.repository.RelationShipRepository;
import com.dzsw.service.IBizService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author sunz
 * @version 1.0
 * @CreateTime 2021-04-26-16:59
 */
@Service
//@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class BizServiceImpl implements IBizService {

    @Resource
    private DeptRepository deptRepository;

    @Resource
    private RelationShipRepository relationShipRepository;

    @Override
    public void create() {
        Dept CEO = Dept.builder().deptName("CEO").build();
        Dept dept1 = Dept.builder().deptName("设计部").build();
        Dept dept11 = Dept.builder().deptName("设计1组").build();
        Dept dept12 = Dept.builder().deptName("设计2组").build();

        Dept dept2 = Dept.builder().deptName("技术部").build();
        Dept dept21 = Dept.builder().deptName("前端技术部").build();
        Dept dept22 = Dept.builder().deptName("后端技术部").build();
        Dept dept23 = Dept.builder().deptName("测试技术部").build();
        List<Dept> depts = new ArrayList<>(Arrays.asList(CEO, dept1, dept11, dept12, dept2, dept21, dept22, dept23));
        deptRepository.saveAll(depts);

        RelationShip relationShip1 = RelationShip.builder().parent(CEO).child(dept1).build();
        RelationShip relationShip2 = RelationShip.builder().parent(CEO).child(dept2).build();
        RelationShip relationShip3 = RelationShip.builder().parent(dept1).child(dept11).build();
        RelationShip relationShip4 = RelationShip.builder().parent(dept1).child(dept12).build();
        RelationShip relationShip5 = RelationShip.builder().parent(dept2).child(dept21).build();
        RelationShip relationShip6 = RelationShip.builder().parent(dept2).child(dept22).build();
        RelationShip relationShip7 = RelationShip.builder().parent(dept2).child(dept23).build();
        List<RelationShip> relationShips = new ArrayList<>(Arrays.asList(relationShip1, relationShip2, relationShip3, relationShip4, relationShip5
                , relationShip6, relationShip7));
        relationShipRepository.saveAll(relationShips);
    }

    @Override
    public RelationShip getRelationShipById(Long id) {
        Optional<RelationShip> byId = relationShipRepository.findById(id);
        return byId.orElse(null);
    }

    @Override
    public Dept getDeptById(Long id) {
        Optional<Dept> dept = deptRepository.findById(id);
        return dept.orElse(null);
    }

    @Override
    public void deleteRelationShip(Long id) {
        relationShipRepository.deleteById(id);
    }

    @Override
    public void deleteDept(Long id) {
        deptRepository.deleteById(id);
    }

    @Override
    public void deleteAll() {
        deptRepository.deleteAll();
        relationShipRepository.deleteAll();
    }

    private Integer num = 0;

    @Neo4j
    @Override
    public Integer getNum(String s, Integer o) {
        String[] ss = new String[0];
        System.out.println(s + " --- " + o);
        System.out.println(ss[10]);
//        throw new RuntimeException("======================== THIS IS EXCEPTION ======================");
        return num++;
    }
}
